<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文字转语音</title>
    <style>
        body {
            font-family: "Helvetica Neue", Arial, sans-serif;
            background: #f4f7f9;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        form {
            background: white;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            text-align: left;
        }

        textarea {
            width: 100%;
            height: 120px;
            padding: 12px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
            color: #333;
            box-sizing: border-box;
            margin-bottom: 15px;
        }

        textarea:focus {
            border-color: #4caf50;
            outline: none;
            box-shadow: 0 0 5px rgba(76, 175, 80, 0.5);
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #4caf50;
            border: none;
            border-radius: 8px;
            color: white;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:disabled {
            background-color: #ddd;
            cursor: not-allowed;
        }

        button:hover:not(:disabled) {
            background-color: #45a049;
        }

        hr {
            border: none;
            height: 1px;
            background: #ddd;
            margin: 20px 0;
        }

        #ttsResponse {
            margin-top: 10px;
            text-align: center;
        }

        audio {
            width: 100%;
            border-radius: 8px;
        }

        .loading {
            font-size: 14px;
            color: #666;
            margin-top: 10px;
        }
    </style>
</head>
<body>

<form id="ttsForm">
    <label for="ttsTextarea" style="font-weight: bold; color: #555;">请输入文字描述：</label>
    <textarea name="input" id="ttsTextarea" placeholder="请输入文字" required></textarea>
    <button type="submit" id="submitBtn">生成音频</button>
    <hr>
    <div id="ttsResponse"></div>
</form>

<script src="./jquery.min.js"></script>
<script>
    $("#ttsForm").submit(function(event) {
        event.preventDefault();
        if ($("#ttsTextarea").val().length < 5) {
            alert("文本太短了");
        } else {
            $("#submitBtn").prop('disabled', true);

            $("#ttsResponse").html('<div class="loading">音频生成中，请稍候...</div>');

            $.post("/ai/tts", $("#ttsForm").serialize(), function(resp) {
                $("#ttsResponse").html(`
                    <audio controls>
                        <source src="${resp}" type="audio/mpeg">
                    </audio>
                `);
            }).always(function() {
                $("#submitBtn").prop('disabled', false);
            });
        }
    });
</script>

</body>
</html>
